<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
    <title>员工管理</title>
    <%@include file="../common/header.jsp"%>
    <script>
        $(function () {
            //超级管理点击事件
            var roleDiv;
            if (${entity.admin}){
                $("#admin").prop("checked",true);
                roleDiv = $("#role").detach();
            }
            $("#admin").click(function () {
                if (this.checked){//选中之后再删除
                    //remove()全部删除,事件也删除
                    //detach()删除元素,却保留事件
                    roleDiv = $("#role").detach();
                }else {
                    //向上找一个div元素,在其后面放置一个roleDiv
                    $(this).closest("div").after(roleDiv);
                    console.log(this);
                    //<input id="admin" name="admin" type="checkbox">
                }
            })
        })
    		/*提交表单后自动选中已经分配的角色,并且提交表单*/
        $(function() {
            $("#btn_submit").click(function () {
                console.log("-------------");
                $(".selfRoles option").prop("selected", true);
                $("#editForm").submit();
            });
        });
        //角色回显
        //获取右边的全部元素
        //获取左边员工本身的角色
        //如果相等就删除左边的元素
        $(function () {
            var ids = $.map($(".selfRoles option"),function (items) {
                return items.value;
            })
            var allRoles = $(".allRoles option");
            $.each(allRoles,function (index,item) {
                if ($.inArray(item.value,ids)>=0){
                    $(item).remove();
                }
            })
        })
    		/*//1: 页面加载完毕之后, 将自己的角色[id]全部获取出来, 放置到一个数组中
    		//参数1:操作数组
    		//参数2:回调方法, 目的是后去遍历option数组中每个元素的value值
    		//结果: 自己角色id数组
    		var ids = $.map( $(".selfRoles option"), function(option){
				  return option.value;
			});
    		//2:将系统的角色[id]也全部获取出来
    		var allRoles = $(".allRoles option");
    		//遍历数组
    		//参数1:数组
    		//参数2:回调方法
    		$.each( allRoles, function(index, item){
        		//3:遍历系统角色[id]一个个去跟 第一步获取到数组对比
        		///判断元素是否在数组中
        		//参数1:判断元素
        		//参数2:数组
        		//返回: 如果存在, 返回数组索引, 不存在返回-1
        		if($.inArray(item.value, ids) >= 0){
	    			//3.1: 如果存在, 删除当前比较系统角色
	    			$(item).remove();
        		}else{
        			//3.2: 如果不存在, 保留
        		}
    		});
*/
    		//解决超管角色下拉框隐藏问题
    		if($("#admin").prop("checked")){
    			 roleDiv = $("#role").detach();
    		}
        $(function () {
    	    function moveAll(srcId,tarId) {
    	    //$("#select1 option").appendTo($("#select2"));
            $("."+srcId+" option").appendTo($("."+tarId));
            //将selected中所有的option都取出来
        }
        function moveSelected(srcId,tarId) {
    	    //将option中本选中的选项选择出来
            $("."+srcId+" option:selected").appendTo($("."+tarId));
        }
        })
    </script>

</head>
<body>
<!--左侧菜单回显变量设置-->
<c:set var="currentMenu" value="employee"/>
<div class="container " style="margin-top: 20px">
    <%@include file="../common/top.jsp"%>
    <div class="row">
        <div class="col-sm-2">
            <%@include file="../common/menu.jsp"%>
        </div>
        <div class="col-sm-10">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">员工编辑</h1>
                </div>
            </div>
            <div class="row col-sm-10">
                <form class="form-horizontal" action="/employee/saveOrUpdate" method="post" id="editForm">
                    <input type="hidden" value="${entity.id}" name="id">
                    <div class="form-group" >
                        <label for="name" class="col-sm-2 control-label">用户名：</label>
                        <div class="col-sm-6">
                        <input type="text" class="form-control" id="name" name="name" value="${entity.name}" placeholder="请输入用户名">
                        </div>
                    </div>
                    <c:if test="${empty entity}"><%--entity != null--%>
                        <div class="form-group">
                            <label for="password" class="col-sm-2 control-label">密码:</label>
                            <div class="col-sm-6">
                                <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="repassword" class="col-sm-2 control-label">验证密码:</label>
                            <div class="col-sm-6">
                                <input type="password" class="form-control" id="repassword" placeholder="请输入密码">
                        </div>
                        </div>
                    </c:if>
                   <%-- <c:if test="${empty entity}">
                    <div class="form-group">
                        <label for="password" class="col-sm-2 control-label">密码：</label>
                        <div class="col-sm-6">
                        <input type="password" class="form-control" id="password"  name="password"  placeholder="请输入密码">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="repassword" class="col-sm-2 control-label">验证密码：</label>
                        <div class="col-sm-6">
                        <input type="password" class="form-control" id="repassword" placeholder="再输入一遍密码">
                        </div>
                    </div>
                    </c:if>--%>
                    <div class="form-group">
                        <label for="email" class="col-sm-2 control-label">Email：</label>
                        <div class="col-sm-6">
                        <input type="text" class="form-control" id="email" name="email" value="${entity.email}" placeholder="请输入邮箱">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="age" class="col-sm-2 control-label">年龄：</label>
                        <div class="col-sm-6">
                        <input type="text" class="form-control" id="age" name="age" value="${entity.age}" placeholder="请输入年龄">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="dept" class="col-sm-2 control-label">部门</label>
                        <div class="col-sm-6">
                            <select class="form-control" id="dept"name="dept.id">
                            <c:forEach items="${depts}" var="d">
                                <option value="${d.id}">${d.name}</option>
                            </c:forEach>
                            </select>
                            <script>
                                $("#dept option[value='${entity.dept.id}']").prop("selected",true);
                            </script>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="admin" class="col-sm-2 control-label">超级管理员：</label>
                        <label class="checkbox-inline" style="margin-left: 15px;">
                            <input type="checkbox" id="admin" name="admin" >
                        </label>
                        <script>
                            <c:if test="${entity.admin}">
                                 $("#admin").prop("checked", true);
                            </c:if>
                        </script>
                    </div>
                    <div class="form-group" id="role">
                        <div>
                            <label for="role" class="control-label" style="margin-left: 60px">角色：</label>
                        </div>
                        <div class="row" style="margin-top: 10px">
                            <%--将controller页面里的查询出的roles值循环放到页面上--%>
                            <div class="col-sm-4 col-sm-offset-1">
                                <select multiple class="form-control allRoles" size="15">
                                    <c:forEach items="${roles}" var="role">
                                        <option value="${role.id}">${role.name}</option>
                                    </c:forEach>
                                </select>
                            </div>
                            <div class="col-sm-2" style="margin-top: 60px;" align="center">
                                <div >
                                    <a type="button" class="btn btn-info  "  style="margin-top: 10px"
                                       onclick="moveSelected('allRoles', 'selfRoles')">&nbsp;&gt;&nbsp;</a>
                                    <br>
                                    <a type="button" class="btn btn-info " style="margin-top: 10px"
                                       onclick="moveSelected('selfRoles', 'allRoles')">&nbsp;&lt;&nbsp;</a>
                                    <br>
                                    <a type="button" class="btn btn-info " style="margin-top: 10px"
                                       onclick="moveAll('allRoles', 'selfRoles')">&gt;&gt;</a>
                                    <br>
                                    <a type="button" class="btn btn-info " style="margin-top: 10px"
                                       onclick="moveAll('selfRoles', 'allRoles')">&lt;&lt;</a>
                                </div>
                            </div>
                                <div class="col-sm-4">
                                    <select multiple class="form-control selfRoles" size="15" name="ids">
                                    <c:forEach items="${entity.roles}" var="role">
                                        <option value="${role.id}">${role.name}</option>
                                        <script>
                                            $(".allRoles option[value='${role.id}']").remove();
                                        </script>
                                    </c:forEach>
                                    </select>
                                </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-6">
                            <%--将原来的保存或者修改改为事件提交,在提交时完成一些其他的操作--%>
                            <button id="btn_submit" type="button" class="btn btn-default">保存</button>
                            <button type="reset" class="btn btn-default">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
</body>
</html>